/*
 * File TableTagBean.java in project jade-core
 *
 * (c) Jade Fortune Technology, Inc. 2005 - 2006 All rights reserved.
 * Proprietary and confidential.
 *
 * Created at 2006-11-1 by eric
 */
package com.example.yogurtsystem.utils;

import org.apache.logging.log4j.util.StringMap;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * bean for TableTag
 *
 * @author eric
 */
public class TableTagBean {
    public static final int DEFAULT_PAGE_SIZE = 20;

    private static final String DELIMITER = "\n";

    // 当前页码
    private int m_page;

    // 每页记录条数
    private int m_pageSize = DEFAULT_PAGE_SIZE;

    // 记录总数
    private int m_total;

    // 排序字段
    private String m_orderBy;

    // 是否是升序
    private boolean m_asc;

    // 查询条件map
    private HashMap<String,String> m_condition = new HashMap<>(
    );

    // 本页数据列表
    @SuppressWarnings("rawtypes")
    private List m_list;

    // 是否已经build
    private boolean m_builded = false;

    private String m_conditionParam = null;

    private int m_pageCount;

    private boolean m_canPrev;

    private boolean m_canNext;

    public TableTagBean() {
    }

    public boolean getAsc() {
        return m_asc;
    }

    public void setAsc(boolean asc) {
        ensureNotBuilded();
        m_asc = asc;
    }

    public HashMap<String,String> getCondition() {
        return m_condition;
    }

    public void setCondition(HashMap condition) {
        ensureNotBuilded();
        m_condition = condition;
    }

    public String getOrderBy() {
        return m_orderBy;
    }

    public void setOrderBy(String orderBy) {
        ensureNotBuilded();
        m_orderBy = orderBy;
    }

    public int getPage() {
        return m_page;
    }

    public void setPage(int page) {
        ensureNotBuilded();
        m_page = page;
    }

    public int getPageSize() {
        return m_pageSize;
    }

    public void setPageSize(int pageSize) {
        ensureNotBuilded();
        m_pageSize = pageSize;
    }

    public int getTotal() {
        return m_total;
    }

    public void setTotal(int total) {
        ensureNotBuilded();
        m_total = total;
    }

    @SuppressWarnings("rawtypes")
    public List getList() {
        return m_list;
    }

    @SuppressWarnings("rawtypes")
    public void setList(List list) {
        ensureNotBuilded();
        m_list = list;
    }

    public void ensureNotBuilded() {
        if (m_builded) {
            throw new IllegalStateException(
                    "Can't set attribute to TableTagBean after build() is called.");
        }
    }

    public boolean getCanNext() {
        return m_canNext;
    }

    public boolean getCanPrev() {
        return m_canPrev;
    }

    public String getConditionParam() {
        return m_conditionParam;
    }

    public int getPageCount() {
        return m_pageCount;
    }
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("[TableTagBean]\r\n");
        sb.append("m_page: " + m_page + "\r\n");
        sb.append("m_pageSize :" + m_pageSize + "\r\n");
        sb.append("m_total :" + m_total + "\r\n");
        sb.append("m_orderBy :" + m_orderBy + "\r\n");
        sb.append("m_asc :" + m_asc + "\r\n");
        sb.append("m_condition :" + m_condition + "\r\n");
        sb.append("m_list(size) :" + (m_list == null ? 0 : m_list.size()) + "\r\n");
        sb.append("m_builded :" + m_builded + "\r\n");
        sb.append("m_conditionParam :" + m_conditionParam + "\r\n");
        sb.append("m_pageCount :" + m_pageCount + "\r\n");
        sb.append("m_canPrev :" + m_canPrev + "\r\n");
        sb.append("m_canNext :" + m_canNext + "\r\n");

        return sb.toString();
    }

    @SuppressWarnings("rawtypes")
    public static TableTagBean getFromExt(HttpServletRequest request) {
//	    String start = request.getParameter("start");
//	    String limit = request.getParameter("limit");
//	    String asc = request.getParameter("dir");
//	    String orderBy = request.getParameter("sort");
        String start = request.getParameter("pageNum");
        String limit = request.getParameter("pageSize");
        String asc = request.getParameter("asc");
        String orderBy = request.getParameter("orderBy");
        int page = (start != null) ? Integer.parseInt(start) : 0;
        int pageSize = (limit != null) ? Integer.parseInt(limit) : 0;
        boolean isAsc = "ASC".equalsIgnoreCase(asc);
        if ("true".equalsIgnoreCase(asc))
            isAsc = true;
        else if ("false".equalsIgnoreCase(asc))
            isAsc = false;

        TableTagBean ttb = new TableTagBean();
        ttb.setPage(page);
        ttb.setPageSize(pageSize);
        ttb.setOrderBy(orderBy);
        ttb.setAsc(isAsc);


        Map<String, String[]> m = (Map<String, String[]>) request.getAttribute("The-result-after-removing-the-space");
        if (m == null || m.size() == 0) {
            m = request.getParameterMap();
        }

        if (m != null) {
            for (Entry entry : m.entrySet()) {
                String key = (String) entry.getKey();
                Object[] value = (Object[]) entry.getValue();
                ttb.getCondition().put(key, (value[0] == null) ? null : value[0].toString());
            }
        }
        return ttb;
    }

    /**
     * 设置OrderBy的对应字段，以避免查询排序出错
     *
     * @param
     * @param
     */
    public void rebuildOrderBy(HashMap<String, String> orderMapping) {
        if (this.m_orderBy != null) {
            try {
                final String columnName = orderMapping.get(this.m_orderBy);
                if (columnName != null) {
                    if (columnName.length() == 0)
                        this.m_orderBy = null;
                    else
                        this.m_orderBy = columnName;
                }
            } catch (Exception e) {
            }
        }
    }
    /**
     * 升序值
     */
    public static final String PAGE_ASC = "ASC";
    /**
     * 当前页数
     */
    public static final int PAGE = 0;

}
